package client

import (
	"learngo/crawler/engine"
	"learngo/crawler_distribute/config"
	"learngo/crawler_distribute/rpcsupport"
	"log"
)

func ItemSaver(host string) (chan engine.Item, error) {
	// ItemSaver 进行分布式
	// 调用RPC让远程的服务器进行save
	client, err := rpcsupport.NewClient(host)
	if err != nil {
		return nil, err
	}
	// 通过out和engine进行传递
	out := make(chan engine.Item)
	go func() {

		itemCount := 0
		for {
			item := <-out
			log.Printf("Item Saver: got item"+"#%d: %v", itemCount, item)
			itemCount++

			// Call RPC to save item
			result := ""
			err = client.Call(config.ItemSaverRpc, item, &result)
			// go routine 中等待返回，所以不需要异步去Call
			if err != nil {
				log.Print("Item Saver: error"+
					"saving item %v: %v", item, err)
			}
		}
	}()
	return out, nil
}
